package com.p2p.user.controller;

import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.p2p.common.base.BaseController;
import com.p2p.common.constants.Message;
import com.p2p.common.enums.ResponseEnum;
import com.p2p.common.utils.MD5Encryption;
import com.p2p.user.po.Role;
import com.p2p.user.po.User;
import com.p2p.user.po.UserRole;
import com.p2p.user.service.UserRoleService;
import com.p2p.user.service.UserService;
import com.p2p.user.vo.UserRoleVo;

/**
 * Created by Allen on 2017/3/19.
 */
@RestController
@RequestMapping(value = "user")
public class UserController extends BaseController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private UserRoleService userRoleService;

	/**
	 * 获取用户列表
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectUser", method = RequestMethod.GET)
	public Map selectUser(HttpServletRequest request) {
		List<User> userList = userService.selectList(new EntityWrapper<User>());
		Map result = new HashMap();
		result.put("rel", true);
		result.put("msg", "SUCCESS");
		result.put("list", userList);
		result.put("count", userList.size());
		return result;
	}

	/**
	 * 获取用户 By UserId
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectUserById", method = RequestMethod.GET)
	public Message selectUserById(HttpServletRequest request) {
		Long userId = Long.parseLong(request.getParameter("userId"));
		User user = userService.selectOne(new EntityWrapper<User>().eq("id", userId));
		UserRole userRole = userRoleService.selectOne(new EntityWrapper<UserRole>().eq("user_id", userId));
		UserRoleVo userRoleVo = new UserRoleVo();
		userRoleVo.setUser(user);
		Role role = new Role();
		role.setId(userRole.getRoleId());
		userRoleVo.setRole(role);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), userRoleVo);
	}

	/**
	 * 获取用户 By UserId
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "selectNowUser", method = RequestMethod.GET)
	public Message selectNowUser(HttpServletRequest request) {
		Long userId = getUserId();
		User user = userService.selectOne(new EntityWrapper<User>().eq("id", userId));
		user.setPassword(null);

		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg(), user);
	}

	/**
	 * 更新用户信息
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateNowUser", method = RequestMethod.POST)
	public Message updateNowUser(HttpServletRequest request, User user) {
		try {
			if (StringUtils.isNotEmpty(user.getPassword())) {
				if (user.getPassword().length() < 6) {
					return new Message(4405, "密码长度小于6位");
				}
				user.setPassword(MD5Encryption.encrypt(user.getPassword(), user.getUsername()));
			} else {
				user.setPassword(null);
			}
			userService.updateById(user);
			return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());

		} catch (Exception e) {
			return new Message(ResponseEnum.SERVER_ERROR.getCode(), ResponseEnum.SERVER_ERROR.getMsg());
		}

	}

	/**
	 * 增加用户列表
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "insertUser", method = RequestMethod.POST)
	public Message insertUser(HttpServletRequest request, User user) {
		Long roleId = Long.parseLong(request.getParameter("roleId"));
		Timestamp timestamp = new Timestamp(System.currentTimeMillis());

		if (userService.selectOne(new EntityWrapper<User>().eq("username", user.getUsername())) != null) {
			return new Message(ResponseEnum.USERNAME_IS_EXIST.getCode(), ResponseEnum.USERNAME_IS_EXIST.getMsg());
		}

		user.setPassword(MD5Encryption.encrypt(user.getPassword(), user.getUsername()));
		user.setCreateTime(timestamp);
		userService.insert(user);

		UserRole userRole = new UserRole();
		userRole.setRoleId(roleId);
		userRole.setUserId(user.getId());
		userRole.setStatus(1);
		userRole.setCreateTime(new Timestamp(System.currentTimeMillis()));
		userRoleService.insert(userRole);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 更新用户信息
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "updateUser", method = RequestMethod.POST)
	public Message updateUser(HttpServletRequest request, User user) {
		Long roleId = Long.parseLong(request.getParameter("roleId"));
		if (StringUtils.isNotEmpty(user.getPassword())) {
			user.setPassword(MD5Encryption.encrypt(user.getPassword(), user.getUsername()));
		}
		userService.updateById(user);
		UserRole userRole = userRoleService.selectOne(new EntityWrapper<UserRole>().eq("user_id", user.getId()));
		userRole.setRoleId(roleId);
		userRoleService.updateById(userRole);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 逻辑删除用户
	 *
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "deleteUser", method = RequestMethod.GET)
	public Message deleteUser(HttpServletRequest request) {
		Long userId = Long.parseLong(request.getParameter("userId"));
		User user = userService.selectById(userId);
		user.setStatus(-1);
		userService.updateById(user);
		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}

	/**
	 * 重置密码
	 *
	 * @param
	 * @return
	 */
	@RequestMapping(value = "updatePassword", method = RequestMethod.GET)
	public Message updatePassword(HttpServletRequest request) {

		return new Message(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMsg());
	}
}
